﻿using AutoMapper;
using Modules.System.Dept;
using PmSoft;
using System.ComponentModel.DataAnnotations;
using static Modules.Project.ProjectConstants;

namespace ManagementApi.Models.System.Dept
{
    /// <summary>
    /// 主管单位视图对象
    /// </summary>
    public class DeptDto
    {

        /// <summary>
        /// 主管单位Id 
        ///</summary>
        public int DeptId { get; set; }
        /// <summary>
        /// 行业分类
        /// </summary>
        public 行业主管部门 IndustryType { get; set; }
        /// <summary>
        /// 行业分类
        /// </summary>
        public string IndustryTypeName { get; set; }
        /// <summary>
        /// 主管单位名称 
        ///</summary>
        public string? Name { get; set; }

        /// <summary>
        /// 上级主管单位名称
        /// </summary>
        public string? ParentName { get; set; }

        /// <summary>
        /// 组织机构代码 
        ///</summary>
        public string? Code { get; set; }

        /// <summary>
        /// 归属省名称
        ///</summary>
        public string? ProvinceName { get; set; }

        /// <summary>
        /// 归属地市名称
        ///</summary>
        public string? CityName { get; set; }

        /// <summary>
        /// 归属区县名称 
        ///</summary>
        public string? AreaName { get; set; }

        /// <summary>
        /// 归属省代码 
        ///</summary>
        public int? ProvinceCode { get; set; }
        /// <summary>
        /// 归属地市代码 
        ///</summary>
        public int? CityCode { get; set; }
        /// <summary>
        /// 归属区县代码 
        ///</summary>
        public int? AreaCode { get; set; }

        /// <summary>
        /// 行政级别
        ///</summary>
        public int? Level { get; set; }
        /// <summary>
        /// 行政级别 1：部、2：省、3：市、4：县 
        ///</summary>
        public string? LevelName { get; set; }

        /// <summary>
        /// 创建时间 
        /// 默认值: CURRENT_TIMESTAMP
        ///</summary>
        public DateTime CreateTime { get; set; }

    }

    public static partial class SysDeptExceptions
    {
        public static DeptDto AsDeptDto(this SysDept dept)
        {
            var mapper = ServiceAccessor.GetService<IMapper>();
            DeptDto dto = new DeptDto();
            mapper.Map(dept, dto);
            dto.ProvinceName = dept.GetProvinceName();
            dto.CityName = dept.GetCityName();
            dto.AreaName = dept.GetAreaName();
            dto.LevelName = dept.Level.GetDescription();// (dept.Level);
            dto.IndustryTypeName = dept.IndustryType.GetDescription();
            return dto;

        }

        public static IEnumerable<DeptDto> AsDeptDtos(this IEnumerable<SysDept> depts)
        {
            return depts.Select(m => m.AsDeptDto());

        }

        /// <summary>
        /// 下拉列表
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static IEnumerable<LabelValue<int>> AsPullDownDto(this IEnumerable<SysDept> data)
        {
            List<LabelValue<int>> dto = new List<LabelValue<int>>();
            foreach (var item in data)
            {
                LabelValue<int> dic = new LabelValue<int>()
                {
                    Value = item.DeptId,
                    Label = item.Name,
                    Extend1=item.IndustryType

                };
                dto.Add(dic);
            }
            return dto;
        }
    }
}
